﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Handrise.Mobile.BasicModal.Camera;

namespace Handrise.CDCable.Mobile.Business
{
    /// <summary>
    /// 巡视信息服务
    /// </summary>
    public class PatrolInfoService
    {
        /// <summary>
        /// 获取需要上报的巡视信息
        /// </summary>
        /// <param name="patrolLogs"></param>
        /// <param name="patrolDetails"></param>
        /// <returns></returns>
        public bool GetNeedReportPatrolInfo(string userName,out List<PatrolLog> patrolLogs, out List<PatrolDetail> patrolDetails)
        {
            try
            {
                patrolLogs = AppLogic.dataService.Find<PatrolLog>("ReportState ='未上报' and UserName ='" + userName + "'", "");
                patrolDetails = AppLogic.dataService.Find<PatrolDetail>("ReportState ='未上报' and UserName ='" + userName + "'", "");
                return true;
            }
            catch (Exception e)
            {
                patrolLogs = new List<PatrolLog>();
                patrolDetails = new List<PatrolDetail>();
                return false;
            }


        }

        /// <summary>
        /// 删除对应计划下的巡视详情
        /// </summary>
        /// <param name="planID"></param>
        /// <returns></returns>
        public bool DeletePlanInfoByPlan(string planID)
        {
            //删除巡视日志
            AppLogic.dataService.Delete<PatrolLog>("PlanID ='" + planID + "'", null) ;

            //获取所有对应计划的巡视详情
            List<PatrolDetail> patrolDetails = GetAllPatrolDetailByPlan(planID) ;

            //删除巡视详情下的文件信息和文件
            foreach (PatrolDetail patrolDetail in patrolDetails)
            {
                LinkFileService linkFileService = new LinkFileService();
                linkFileService.DeleteLinkFileByLinkID("'" + patrolDetail.PatrolDetailID + "'");
            }
 
            //删除巡视详情
            AppLogic.dataService.Delete<PatrolDetail>("PlanID ='" + planID + "'", null);
            
            return true;
        }

        /// <summary>
        /// 通过巡视计划获取对应的巡视详情
        /// </summary>
        /// <param name="planID"></param>
        /// <returns></returns>
        public List<PatrolDetail> GetAllPatrolDetailByPlan(string planID)
        {
            return AppLogic.dataService.Find<PatrolDetail>("PlanID = '" + planID + "'", "");
        }

        /// <summary>
        /// 通过巡视设备获取设备详情
        /// </summary>
        /// <param name="planDeviceID"></param>
        /// <returns></returns>
        public List<PatrolDetail> GetPatrolDetailByPlanDeviceID(string planDeviceID)
        {
            return AppLogic.dataService.Find<PatrolDetail>("PlanDeviceID ='" + planDeviceID + "'", "");
        }

        /// <summary>
        /// 添加一个巡视详情
        /// </summary>
        /// <param name="patrolDetail"></param>
        public void AddProtolDetail(PatrolDetail patrolDetail,PlanDevice planDevice,PatrolItem patrolItem)
        {
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            parameters.Add("LogDateTime", DateTime.Today);
            //先判断当日的巡视日志是否存在
            PatrolLog patrolLog = AppLogic.dataService.GetFirstEntity<PatrolLog>("UserName = '" + AppLogic.LoginUser.UserName + "' and PlanId = '" + patrolDetail.PlanID + "' and LogDateTime =@LogDateTime", "", parameters);

            //如果不存在，则创建一个巡视日志
            if (patrolLog == null)
            {
                patrolLog = new PatrolLog();
                patrolLog.PatrolLogID = Guid.NewGuid().ToString();
                patrolLog.LogDateTime = DateTime.Today;
                patrolLog.PlanID = patrolDetail.PlanID;
                patrolLog.ReportState = "未上报";
                patrolLog.UserName = AppLogic.LoginUser.UserName ;
                AppLogic.dataService.Add<PatrolLog>(patrolLog);
            }

            if (planDevice != null)
            {
                //如果关联设备是隧道和线路需要计算长度
                DeviceService deviceService = new DeviceService();
                string length = "";
                string name = "";
                string type = "";
                if(deviceService.GetLineOrTunnelInfo(planDevice.DeviceID,out length,out name,out type))
                {
                    patrolDetail.ParentDeviceLength = length;
                    patrolDetail.ParentDeviceName = name;
                    patrolDetail.ParentDeviceType = type ;
                }

                patrolDetail.DeviceID = planDevice.DeviceID;
                patrolDetail.DeviceName = planDevice.DeviceName;
                patrolDetail.DeviceType = planDevice.DeviceType;
                patrolDetail.PlanDeviceID = planDevice.PlanDeviceID;
            }

            if (patrolItem != null)
            {
                patrolDetail.PatrolItemID = patrolItem.PatrolItemID;
                patrolDetail.PatrolItemName = patrolItem.ItemName;
                patrolDetail.WorkType = patrolItem.WorkType;
                patrolDetail.PlanID = planDevice.PlanID;
            }

            patrolDetail.PlanLogID = patrolLog.PatrolLogID;
            patrolDetail.ReportState = "未上报";
            patrolDetail.UserName = AppLogic.LoginUser.UserName;

            AppLogic.dataService.Add<PatrolDetail>(patrolDetail);

        }

        /// <summary>
        /// 更新巡视详情
        /// </summary>
        /// <param name="patrolDetail"></param>
        public void UpdatePatrolDetail(PatrolDetail patrolDetail)
        {
            AppLogic.dataService.Update<PatrolDetail>(patrolDetail);
        }

        /// <summary>
        /// 获取计划中的其他工作类型的巡视详情
        /// </summary>
        /// <param name="planID"></param>
        public PatrolDetail GetOtherWorkTypePatrolDetail(string planID)
        {
            return  AppLogic.dataService.GetFirstEntity<PatrolDetail>("PlanID ='" + planID + "' and WorkType ='其他工作'", "");
        }

        /// <summary>
        /// 更改巡视详情上报状态
        /// </summary>
        /// <param name="state"></param>
        public void UpdateReportState(string state)
        {
            //更新缺陷信息上报状态
            AppLogic.dataService.ExecuteSql("Update PatrolDetail set ReportState ='" + state + "' Where ReportState = '未上报' and UserName = '" + AppLogic.LoginUser.UserName  + "'" );
        }

        /// <summary>
        /// 获取巡视详情
        /// </summary>
        /// <param name="patrolDetail"></param>
        public PatrolDetail GetPatrolDetail(string patrolDetailID)
        {
            return  AppLogic.dataService.GetEntityByKey<PatrolDetail>(patrolDetailID);
        }

        
    }
}
